package com.poorstu.manager.impl;

import java.util.List;

import com.poorstu.dao.FlowDao;
import com.poorstu.dao.RoleDao;
import com.poorstu.manager.FlowManager;
import com.poorstu.model.FlowLineMod;
import com.poorstu.model.FlowMod;
import com.poorstu.model.RoleMod;

public class FlowManagerImpl implements FlowManager {
    
	  private FlowDao flowDao;
	  private RoleDao roleDao;
	public List<FlowMod> selectFlowAll() {
		     
		return flowDao.selectFlowAll();
	}
	
	
	
	
	public FlowMod selectFLowById(int flowId) {
		return flowDao.selcetFlowById(flowId);
	}

	


	public void updateFlow(int flowId, String flowName) {
		
		FlowMod flowMod=flowDao.selcetFlowById(flowId);
		flowMod.setFlowName(flowName);
		flowDao.updateFlow(flowMod);
	}


	
	

	public void saveFlow(FlowMod flowMod) {
		flowDao.savaFlow(flowMod);
		
	}

	


	public void deleteFlow(int flowId) {

		
		FlowMod flowMod=flowDao.selcetFlowById(flowId);
		
		List<FlowLineMod> flowlineList=flowMod.getFlowLineList();
		
		for(int i=0;i<flowlineList.size();i++)
		{
			flowDao.deleteFlowLine(flowlineList.get(i));
		}
		
		flowDao.deleteFlow(flowMod);
	}




	public void updateFlowLine(int[] roleId, int flowId) {
		
		List<FlowLineMod> flowLine=flowDao.selectFlowLineByFlowId(flowId);
		FlowMod flowMod=flowDao.selcetFlowById(flowId);
		
		for(int j=0;j<flowLine.size();j++)
		{  
			//FlowLineMod flowLineMod=new FlowLineMod();
			FlowLineMod  flowLineMod=flowLine.get(j);
			flowDao.deleteFlowLine(flowLineMod);
		}
		for(int i=0;i<roleId.length;i++)
		{
			
			int orders=i;
			RoleMod roleMod=new RoleMod();
			roleMod=roleDao.getRole(roleId[i]);
			FlowLineMod flowLineMod=new FlowLineMod();
			flowLineMod.setFlow(flowMod);
			flowLineMod.setOrders(orders);
			flowLineMod.setRole(roleMod);
			flowDao.saveFlowLine(flowLineMod);
			
			}
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	public void setFlowDao(FlowDao flowDao) {
		this.flowDao = flowDao;
	}
	

}
